import { createStore } from 'vuex'
import api from '@/api/index.js'

export default createStore({
  state: {
    playList: [{
      al: {
        id: 39745017,
        name: "任你",
        picUrl: "http://p3.music.126.net/k7vpV2CVLB_3ja9VTefPlw==/109951163361842914.jpg"

      }
    }],
    playCurrentIndex: 0,
    lyric: {},
    currentTime: 0,
    intervalId: 0,
    user: {
      isLogin: false,
      account: {},
      userDetail: {}
    },
  },
  getters: {
    lyricList: function (state) {
      // console.log(state.lyric);
      let arr = state.lyric.split(/\n/igs).map((item, i) => {
        let min = parseInt(item.slice(1, 3))
        let sec = parseInt(item.slice(4, 6))
        let mill = parseInt(item.slice(7, 10))

        return {
          min, sec, mill,
          lyric: item.slice(11, item.lenght),
          content: item,
          time: mill + sec * 1000 + min * 1000 * 60,
          playTime: min + ':' + sec
        }
      })
      arr.forEach((item, i) => {
        if (i == 0) {
          item.pre = 0
        } else {
          item.pre = arr[i - 1].time
        }
      })
      return arr
    }
  },
  mutations: {
    setPlayList: function (state, value) {
      state.playList = value
    },
    pushPlayList: function (state, value) {
      state.playList.push(value)
    },
    setPlay: function (state, value) {
      state.playCurrentIndex = value
    },
    setLyric: function (state, value) {
      state.lyric = value
      console.log(state.lyric);
    },
    setCurrentTime(state, value) {
      state.currentTime = value
    },
    setUser(state, value) {
      state.user = value
    },
  },
  actions: {
    // 获取歌词
    async reqLyric(content, payload) {
      let result = await api.getLyric(payload.id)
      content.commit('setLyric', result.data.lrc.lyric)
      console.log(result);

    },

    // 手机登录
    async phoneLogin(content, payload) {

      let result = await api.phoneLogin(payload.phone, payload.password)
      if (result.data.code == 200) {
        content.state.user.isLogin = true
        content.state.user.account = result.data.account
        let userDetail = await api.userDetail(result.data.account.id)
        content.state.user.userDetail = userDetail
        localStorage.userData = JSON.stringify(content.state.user)
        console.log(userDetail);
        content.commit('setUser', content.state.user)
      }
      console.log(result);
      return result

    }
  },
  modules: {
  }
})
